package com.inspinia.base.util;
/*
 * #%L 
 * Node Agent for cloudconductor framework 
 * %% 
 * Copyright (C) 2013 - 2014 Cinovo AG 
 * %% 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0 
 *  
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * #L% 
 */

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.Set;


/**
 * Copyright 2013 Cinovo AG<br>
 * <br>
 *
 * @author psigloch
 * @link http://www.programcreek.com/java-api-examples/index.php?api=java.nio.file.attribute.GroupPrincipal
 */
public class FileHelper {

    private FileHelper() {
        // prevent initialization
    }

    /**
     * @param localFile the local file
     * @return the file attribute view
     */
    public static PosixFileAttributeView getFileAttributes(File localFile) {
        Path localFilePath = Paths.get(localFile.getAbsolutePath());
        return java.nio.file.Files.getFileAttributeView(localFilePath, PosixFileAttributeView.class);
    }

    /**
     * Check if a file has a specific file mode or not
     *
     * @param file     the file to check
     * @param fileMode the file mode to check
     * @return file mode is equal or not
     * @throws IOException on error
     */
    public static boolean isFileMode(File file, String fileMode) throws IOException {
        PosixFileAttributeView view = FileHelper.getFileAttributes(file);
        Set<PosixFilePermission> fileModeSet = PosixFilePermissions.fromString(fileMode);
        if (view.readAttributes().permissions().equals(fileModeSet)) {
            return true;
        }
        return false;
    }

    /**
     * Checks whether a files owner and group are correct
     *
     * @param file  the file to check
     * @param owner the owner
     * @param group the group
     * @return owner and group of file are correct or not
     * @throws IOException on error
     */
    public static boolean isFileOwner(File file, String owner, String group) throws IOException {
        PosixFileAttributeView view = FileHelper.getFileAttributes(file);
        if (!view.readAttributes().owner().getName().equals(owner)) {
            return false;
        }
        if (!view.readAttributes().group().getName().equals(group)) {
            return false;
        }
        return true;
    }

    /**
     * @param localFile the local file to use chown on
     * @param owner     the file owner to set
     * @param group     the file group to set
     * @throws IOException if chown couldn't be edited
     */
    public static void chown(File localFile, String owner, String group) throws IOException {
        PosixFileAttributeView view = FileHelper.getFileAttributes(localFile);
        UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
        UserPrincipal fileOwner = lookupService.lookupPrincipalByName(owner);
        GroupPrincipal fileGroup = lookupService.lookupPrincipalByGroupName(group);
        view.setOwner(fileOwner);
        view.setGroup(fileGroup);
    }

    /**
     * @param localFile the local file to use chmod on
     * @param fileMode  the filemode to set in PosixFilePermissions string type -> 764 = rwxrw-r--
     * @throws IOException if chmod couldn't be edited
     */
    public static void chmod(File localFile, String fileMode) throws IOException {
        PosixFileAttributeView view = FileHelper.getFileAttributes(localFile);
        Set<PosixFilePermission> fileModeSet = PosixFilePermissions.fromString(fileMode);
        view.setPermissions(fileModeSet);
    }

    /**
     * mysql用户名
     */
    public static final String USER_MYSQL = "mysql";

    /**
     * 切换文件及其父文件夹的用户为mysql。
     *
     * @param filePath 文件路径
     * @throws IOException
     */
    public static void changeFileAndParentOwner2Mysql(String filePath) throws IOException {
        //目前测试只有在Linux下才生效。
        if (OSUtils.MAC || OSUtils.WINDOWS) {
            return;
        }
        File file = new File(filePath);
        FileHelper.chown(file.getParentFile(), USER_MYSQL, USER_MYSQL);
        FileHelper.chown(file, USER_MYSQL, USER_MYSQL);
    }
}
  
